package com.xr.sy.modules.message.controller.api;

import com.xr.common.annotation.LogOperation;
import com.xr.common.constant.Constant;
import com.xr.common.page.PageData;
import com.xr.common.utils.Result;
import com.xr.common.validator.AssertUtils;
import com.xr.common.validator.ValidatorUtils;
import com.xr.common.validator.group.AddGroup;
import com.xr.common.validator.group.DefaultGroup;
import com.xr.common.validator.group.UpdateGroup;
import com.xr.sy.modules.message.dto.SysMailDTO;
import com.xr.sy.modules.message.service.SysMailService;
import com.xr.sy.modules.message.service.SysMailTemplateService;
import com.xr.sy.modules.security.service.ShiroService;
import com.xr.sy.modules.security.user.UserDetail;
import com.xr.sy.modules.sys.dto.SysUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 邮箱
 *
 * @author Joe 825525632@qq.com
 * @since 1.0.0 2019-11-26
 */
@RestController
@RequestMapping("api/mail")
@Api(tags = "邮箱")
public class ApiMailController {
    @Autowired
    private SysMailService sysMailService;
    @Autowired
    private ShiroService shiroService;

    @GetMapping("verifyCode")
    @ApiOperation(value = "邮箱验证码")
    @ApiImplicitParam(paramType = "query", dataType="string", name = "email", required = true)
    public Result<String> verifyCode(HttpServletResponse response, String email)throws IOException {
        if(StringUtils.isNotBlank(email)){
            boolean flag = false;
            try {
                flag = sysMailService.sendVerifyCode(email);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(flag){
                return new Result<String>().ok("发送验证码成功");
            }else {
                return new Result<String>().error("邮箱不存在，请联系管理员!");
            }
        }else {
            return new Result<String>().error("请填写邮箱地址");
        }

    }

    @GetMapping("detail")
    @ApiOperation(value = "邮箱验证码")
    public Result<Map<String, Object>> detail(HttpServletRequest httpRequest)throws IOException {
        //从header中获取token
        String token = httpRequest.getHeader(Constant.TOKEN_HEADER);
        SysUserDTO tokenEntity = shiroService.getByToken(token);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
//        Subject subject = SecurityUtils.getSubject();
//        UserDetail userDetail = (UserDetail) subject.getPrincipal();
        Map<String, Object> map = new HashMap<>(3);
        map.put("username", tokenEntity.getEmail());
        map.put("email", tokenEntity.getUsername());
        map.put("date", date);
        return new Result<Map<String, Object>>().ok(map);

    }


}